﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eDoctor.SDK.WeChat;
using Web.Areas.Admin.Lib;

namespace Web.Controllers
{
    public class JavaScriptController : Controller
    {
        /// <summary>
        /// 获取微信公众号的JS-SDK配置
        /// </summary>
        /// <param name="url"></param>
        /// <param name="debug"></param>
        /// <returns></returns>
        public JavaScriptResult jsconfig(string url, string debug = "false")
        {
            var temp = @"wx.config({
                            debug: [_debug_], // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                            appId: '[_appId_]', // 必填，公众号的唯一标识
                            timestamp: [_timestamp_], // 必填，生成签名的时间戳
                            nonceStr: '[_nonceStr_]', // 必填，生成签名的随机串
                            signature: '[_signature_]',// 必填，签名，见附录1
                            jsApiList: [
                                'checkJsApi',
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                'onMenuShareWeibo',
                                'hideMenuItems',
                                'showMenuItems',
                                'hideAllNonBaseMenuItem',
                                'showAllNonBaseMenuItem',
                                'translateVoice',
                                'startRecord',
                                'stopRecord',
                                'onRecordEnd',
                                'playVoice',
                                'pauseVoice',
                                'stopVoice',
                                'uploadVoice',
                                'downloadVoice',
                                'chooseImage',
                                'previewImage',
                                'uploadImage',
                                'downloadImage',
                                'getNetworkType',
                                'openLocation',
                                'getLocation',
                                'hideOptionMenu',
                                'showOptionMenu',
                                'closeWindow',
                                'scanQRCode',
                                'chooseWXPay',
                                'openProductSpecificView',
                                'addCard',
                                'chooseCard',
                                'openCard'
                            ] 
                        });";

            string appid, appsecret, jsapi_ticket, noncestr, signature;
            long timestamp;

            var site=new Site();

            //获取微信公众号配置
            site.GetAppConfig(out appid, out appsecret);

            //获取微信临时票据
            jsapi_ticket = site.GetTicket();

            //根据数据，生成签名
            signature = Call.Signature(jsapi_ticket, url, out noncestr, out timestamp);

            //替换模版中的参数
            temp = temp.Replace("[_debug_]", debug).Replace("[_appId_]", appid).Replace("[_timestamp_]", timestamp.ToString()).Replace("[_nonceStr_]", noncestr).Replace("[_signature_]", signature);

            return JavaScript(temp);
        }

    }
}
